<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

            //
            switch ($this->method()){
                case 'POST':
                    return [
                        'name' => 'required|string|max:255',
                        'password' => 'required|string|min:6',
                        'verif_key' => 'required|string',
                        'verif_code' => 'required|string'
                    ];
                    break;
                case 'PATCH':
                    $userId = Auth::guard('api')->id();
                    return [
                        'name' => 'between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,' . $userId,
                        'email' => 'email',
                        'introduction' => 'max:80',
                        'avatar_image_id' => 'exists:images,id,type,avatar,user_id,' . $userId,
                    ];
                        break;
            }


    }

    public function attributes()
    {
       return [
           'verif_key' => '短信验证码key',
           'verif_code' => '短信验证码'
       ];
    }

    public function messages()
    {
        return [
          'name.unique' => '用户名占用',
          'name.regex' => '用户名只支持中引文',
          'name.between' => '3-25个字符之间',
          'name.required' => '用户名不能为空',

        ];
    }
}
